package com.hddz.comlib;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.util.Log;

import com.hddz.comlib.packet.RFIDPacketReceiver;

public class RFIDSerialHelper {
	private static final String TAG = "RFIDSerialHelper";
	private static final String DEVFILE_PATH = "/proc/driver/as3992";
	private static final String SERIALPORT_PATH = "/dev/ttyS2";
	private DeviceControl mdevCtrl = null;
	private SerialPort mSerialPort = null;
	private FileInputStream mInputStream;
	private ReadThread mReadThread;
	private RFIDPacketReceiver packetReceiver;

	public void setPacketReceiver(RFIDPacketReceiver packetReceiver) {
		this.packetReceiver = packetReceiver;
	}

	/**
	 * 连接NFC串口，开始读取
	 * 
	 * @return
	 */
	public Boolean startRFIDSerial() {
		try {
			Log.d(TAG, "startRFIDSerial");

			mReadThread = new ReadThread();
			mdevCtrl = new DeviceControl(DEVFILE_PATH);
			mSerialPort = new SerialPort(new File(SERIALPORT_PATH), 9600, 0);
			mInputStream = (FileInputStream) mSerialPort.getInputStream();

			mdevCtrl.PowerOnDevice();
			if (mReadThread.isInterrupted()) {
				mReadThread.resume();
			} else {
				mReadThread.start();
			}
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 停止NFC串口
	 * 
	 * @return
	 */
	public Boolean stopRFIDSerial() {
		try {
			Log.d(TAG, "stopRFIDSerial");
			mdevCtrl.PowerOffDevice();
			// if (mReadThread != null) {
			// mReadThread.wait();
			// }
			if (mReadThread != null) {
				mReadThread.interrupt();
				mReadThread = null;
			}
			if (mInputStream != null) {
				mInputStream.close();
				mInputStream = null;
			}
			if (mdevCtrl != null) {
				mdevCtrl.PowerOffDevice();
				mdevCtrl = null;
			}
			if (mSerialPort != null) {
				mSerialPort.close();
				mSerialPort = null;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private Boolean powerOnDevice() {
		try {
			mReadThread = new ReadThread();
			mdevCtrl = new DeviceControl(DEVFILE_PATH);
			mSerialPort = new SerialPort(new File(SERIALPORT_PATH), 9600, 0);
			mInputStream = (FileInputStream) mSerialPort.getInputStream();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	private Boolean powerOffDevice() {
		try {
			if (mReadThread != null) {
				mReadThread.interrupt();
				mReadThread = null;
			}
			if (mInputStream != null) {
				mInputStream.close();
				mInputStream = null;
			}
			if (mdevCtrl != null) {
				mdevCtrl.PowerOffDevice();
				mdevCtrl = null;
			}
			if (mSerialPort != null) {
				mSerialPort.close();
				mSerialPort = null;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private void receivedCNT(String cnt) {
		Log.d(TAG, "Read RFID:" + cnt);
		if (packetReceiver != null) {
			packetReceiver.OnReceivedCNT(cnt);
		}
	}

	class ReadThread extends Thread {
		private byte[] buffer = new byte[64];
		private int size = 0;

		@Override
		public void run() {
			super.run();
			Log.d(TAG, "run start");
			while (!isInterrupted()) {
				Log.d(TAG, "running");
				size = 0;
				try {
					if (mInputStream == null)
						return;
					size = mInputStream.read(buffer);
					if (size >= 10) {
						String cnt = getRFID(buffer, 2);
						receivedCNT(cnt);
					}
				} catch (IOException e) {
					e.printStackTrace();
					return;
				}
			}
			Log.d(TAG, "run stop");
		}

		private String getRFID(byte[] src, int type) {
			int index = 0;
			while (src.length - index >= 13) {
				if (src[index] == 0x02 && src[index + 12] == 0x03) {
					switch (type) {
					case 1:
						break;
					case 2:
						String v_2 = "";
						for (int i = 3; i <= 10; i++) {
							v_2 += (char) src[i];
						}
						int v = Integer.parseInt(v_2, 16);
						return addZeroForNum(v + "", 10);
					case 3:
						break;
					case 4:
						break;
					case 5:
						break;
					}
				}
			}
			return null;
		}

		private String addZeroForNum(String str, int strLength) {
			int strLen = str.length();
			if (strLen < strLength) {
				while (strLen < strLength) {
					StringBuffer sb = new StringBuffer();
					sb.append("0").append(str);// 左补0
					// sb.append(str).append("0");//右补0
					str = sb.toString();
					strLen = str.length();
				}
			}
			return str;
		}
	}
}
